package alingrad.engine.graphics;

import javax.microedition.khronos.opengles.GL10;

import alingrad.engine.objects.TreeNode;

/**
 * Abstract class that contains all the animations for a particular object.
 * Must be overridden or defined in each object's class definition.
 * @author alingrad
 *
 */

public abstract class AnimationHandler extends TreeNode {
	/**
	 * Array of all the sprites for an object.
	 */
	protected Sprite[] mSprites;

	/**
	 * Index of the current frame in the sprite array.
	 */
	private int mFrame;

	/**
	 * Array of all animations for an object.
	 */
	protected Animation[] mAnimations;

	/**
	 * Index of the current animation in the animation array.
	 */
	private int mCurrentAnim;

	/**
	 * Constructs an AnimationHandler.
	 */
	public AnimationHandler() {
	}

	/**
	 * Method in which all the sprites are loaded.
	 */
	public abstract void loadGraphics(GL10 gl);

	/**
	 * method that returns the current sprite to be used.
	 * @return the sprite.
	 */
	public Sprite getCurrentSprite() {
		return mSprites[mFrame];
	}

	/**
	 * Sets the current animation. Will not reset if the animation does not change.
	 * @param animation the animation to set.
	 */
	public void setAnimation(final int animation) {
		if (mAnimations[mCurrentAnim] != mAnimations[animation]) {
			resetAnimation(animation);
		}
	}

	/**
	 * Continues an animation from its last point.
	 * @param animation animation to continue.
	 */
	public void continueAnimation(final int animation) {
		mCurrentAnim = animation;
		mFrame = mAnimations[mCurrentAnim].mFrame;
	}

	/**
	 * Sets and resets the current animation, even if its the same animation.
	 * @param animation animation to reset.
	 */
	public void resetAnimation(final int animation) {
		mCurrentAnim = animation;
		mAnimations[mCurrentAnim].reset();
		mFrame = mAnimations[mCurrentAnim].mFrame;
	}

	/**
	 * Returns the current animation being used.
	 * @return the current animation.
	 */
	public int getCurrentAnimation() {
		return mCurrentAnim;
	}

	/**
	 * Updates to the correct animation.
	 * @param delta Time (in milliseconds) since the last update call.
	 */
	public void update(final long delta) {
		mAnimations[mCurrentAnim].update(delta);
		mFrame = mAnimations[mCurrentAnim].mFrame;
	}
}
